package com.gaoxin.api.contronller.feecontroller;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.gaoxin.api.entity.eduentity.Clazz;
import com.gaoxin.api.entity.feeentity.StuFeeEntity;
import com.gaoxin.api.entity.stuentity.StuInfoEntity;
import com.gaoxin.api.service.feeservice.QueryStuFeeService;
import com.google.common.collect.Maps;

import io.swagger.annotations.Api;

/**
 * 查询学生缴费接口-控制层
 *
 * @author fanyue
 * @email 859485490@qq.com
 * @date 2018-07-12 15:21
 */
@RestController
@RequestMapping("/app/finance")
@Api(tags = "查询学生缴费")
public class QueryStuFeeController {

	Logger log = Logger.getLogger("查询学生缴费~~~~~~~~~~~log~~~~~~~~");
/*	@Autowired
	private StuInfoService stuInfoService;*/
	@Autowired
	private QueryStuFeeService queryStuFeeService;

	/*@RequestMapping(value = "/queryStuFees", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String Test(@RequestBody Map<String,String> paramMap) {
		log.info("查询学生缴费+++++++++" + paramMap.toString());
		String returnStr = "";
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			String stuNumber = paramMap.get("stuNumber");
			String chargeYear = paramMap.get("chargeYear");
			StuInfoEntity stuInfo = stuInfoService.queryStuInfo(stuNumber);
			if (stuInfo == null) {
				retMap.put("code", "100100");
				retMap.put("errmsg", "未查询到该学号对应学生");
				return JSON.toJSONString(retMap);
			}
			Map<String, Object> stuInfoMap = new HashMap<String, Object>();
			stuInfoMap.put("stuNumber", stuInfo.getStuNumber());
			stuInfoMap.put("stuName", stuInfo.getStuName());
			List<StuFeeEntity> queryStuFees = queryStuFeeService.queryStuFees(stuInfo.getStuCode(), chargeYear);
			stuInfoMap.put("arrayList", queryStuFees);
			retMap.put("code", "0");
			retMap.put("results", stuInfoMap);
			returnStr = JSON.toJSONString(retMap);
		} catch (Exception e) {
			log.warning("查询学生缴费异常。。。");
			e.printStackTrace();
			retMap.put("code", "100301");
			retMap.put("errmsg", "查询学生缴费信息错误");
			returnStr = JSON.toJSONString(retMap);
		}
		// log.info("returnStr+++++++++" + returnStr);
		return returnStr;
	}*/
	@RequestMapping(value = "/queryStuFees", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String Test(@RequestBody StuInfoEntity stuInfoEntity) {
		StuFeeEntity stuFeeEntity = new StuFeeEntity();
		stuFeeEntity.setChargeYear(stuInfoEntity.getChargeYear());
		String returnStr = "";
		Map<String, Object> maps = Maps.newHashMap();
		try {
			List<StuInfoEntity> qSBInfo = queryStuFeeService.queryStuBasicInfo(stuInfoEntity);
			if (qSBInfo != null) {
				for(int i = 0;i<qSBInfo.size();i++){
					stuFeeEntity.setStuCode(qSBInfo.get(i).getStuCode());
					List<StuFeeEntity> queryStuFees = queryStuFeeService.queryStuFees(stuFeeEntity);
					qSBInfo.get(i).setQueryStuFeesList(queryStuFees);
				}
				maps.put("results", qSBInfo);
				returnStr = JSON.toJSONString(maps);
			}else{
				maps.put("code", "100100");
				maps.put("errmsg", "未查询到该班级/学号对应学生");
				returnStr = JSON.toJSONString(maps);
			}
		} catch (Exception e) {
			log.warning("查询学生缴费异常。。。");
			e.printStackTrace();
			maps.put("code", "100301");
			maps.put("errmsg", "查询学生缴费信息错误");
			returnStr = JSON.toJSONString(maps);
		}
		return returnStr;
	}
	/**
	 * 查询所有的二级院系
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value = "/queryDept", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String queryDept() {
		String jsonData = null;
		try {
			List<Clazz> list = queryStuFeeService.queryDept();
			Map<String, Object> map = Maps.newHashMap();
			if(list.size()!=0&&list!=null){
				map.put("code", "0");
				map.put("results",list);
				jsonData = JSON.toJSONString(map);
			}else{
				map.put("code", "100202");
				map.put("errmsg", "无院系信息！");
				jsonData = JSON.toJSONString(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonData;
	}
	/**
	 * 根据院系查询所属专业信息
	 * @return
	 */
	@RequestMapping(value = "/queryMajor", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String queryMajor(@RequestBody Clazz clazz) {
		String jsonData = null;
		try {
			List<Clazz> major = queryStuFeeService.queryMajor(clazz);
			Map<String, Object> map = Maps.newHashMap();
			if(major!=null){
				map.put("code", "0");
				map.put("results",major);
				jsonData = JSON.toJSONString(map);
			}else{
				map.put("code", "100203");
				map.put("errmsg", "无该院系对应的专业信息！");
				jsonData = JSON.toJSONString(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonData;
	}
	/**
	 * 根据专业查询班级
	 * @param clazz
	 * @return
	 */
	@RequestMapping(value = "/queryClazz", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String queryClazz(@RequestBody Clazz clazz) {
		String jsonData = null;
		try {
			List<Clazz> queryClazz = queryStuFeeService.queryClazz(clazz);
			Map<String, Object> map = Maps.newHashMap();
			if(queryClazz!=null){
				map.put("code", "0");
				map.put("results",queryClazz);
				jsonData = JSON.toJSONString(map);
			}else{
				map.put("code", "100204");
				map.put("errmsg", "无该专业对应的班级信息！");
				jsonData = JSON.toJSONString(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonData;
	}

}
